﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XWG.JWBST.ProcessInterfaces;
using System.IO;
using System.Web;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace XWG.JWBST.BreakRulesProcess
{
    /// <summary>
    /// 违章记录查询
    /// </summary>
    public class Process : IProcess
    {
        public string ProcessData(StreamReader reader, HttpContext context)
        {
            string info = reader.ReadToEnd();
            string json = null;
            info = HttpUtility.UrlDecode(info);
            JObject oka = (JObject)JsonConvert.DeserializeObject(info);
            string userID = oka["userID"].ToString().Replace("\"", string.Empty);
            string Car_no = oka["carno"].ToString().Replace("\"", string.Empty);
            var user = XWG.JWBST.JWBSTBLL.TbBstUserBLL.Load(userID);
            if (user != null)  //确认是否拥有该用户信息
            {
                if (user.Deltag == 0)   //确认该用户是否被删除
                {
                    //int i = XWG.JWBST.JWBSTBLL.TbBstCarBLL.IsAudit(userID,Car_no);   //根据用户ID和车牌号查询车辆审核状态  0 未审核  1审核成功 2审核失败
                    //if (i == 1)  //该车是否通过审核
                    //{
                        List<XWG.JWBST.JWBSTEntity.TbBstBreakRoleRec> breakinfo = new List<JWBSTEntity.TbBstBreakRoleRec>();
                        breakinfo = XWG.JWBST.JWBSTBLL.TbBstBreakRoleRecBLL.SelectCarInfo(userID, Car_no);

                        //记录请求信息
                        JWBSTEntity.TbBstVisits visit = new JWBSTEntity.TbBstVisits();
                        visit.Id = Guid.NewGuid().ToString();
                        visit.Userid = userID;
                        visit.Time = DateTime.Now;
                        visit.Modulename = "BreakRules";
                        visit.Optionname = "违章记录查询";
                        visit.Other = oka.ToString();
                        XWG.JWBST.JWBSTBLL.TbBstVisitsBLL.Insert(visit, "", false);

                        IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
                        json = JsonConvert.SerializeObject(breakinfo,dtConverter);
                        return json;
                    //}
                    //else if (i == 0)
                    //{
                    //    return "该车尚未审核";
                    //}
                    //else
                    //{
                    //    return "该车审核失败";
                    //}
                }
                else
                {
                    return "该用户已被删除";
                }
            }
            else
            {
                return "不存在该用户";
            }
        }
    }
}
